<?php
class ChallengeController extends Zend_Controller_Action{
	

	public function init()
	{
		
		$this->oLoggedInUser = Default_Service_Auth::getLoggedInUser();

		
	}
	
	public function indexAction()
	{
		if(!isset($this->oLoggedInUser)){
			$this->_helper->redirector("index","login","default");
		}
		$id = $this->_getParam("id");
		
		$request = $this->getRequest();
		
		if($request->isPost())
		{
			
			
				$emailAddress = explode(",", $request->getPost('invite'));				
				if(is_array($emailAddress))
				{
						
						for($i=0;$i<count($emailAddress);$i++)
						{
							$oChallenge = new Challenge();
							$oChallenge->setUserId($id)
									   ->setEmail($emailAddress[$i])
									   ->setInvitationDate(date('Y-m-d'))
									   ->save();
							
						}
						
				}
				
				array_unique($emailAddress);
				$mailDetails = array(
	
												"to"				=>	$emailAddress,
												"subject"			=>	"Challenge for team",
												"challengeid"			=>  $oChallenge->getId(),
												
												
											);
	
						$oMail = new Default_Service_Mail();
						$oMail->sendMail("challenge", $mailDetails);
	
						$this->view->info = array(
	                                                    "type"		=>  "success",
	                                                    "message"	=>	Default_Service_Message::MAIL_SENT_SUCCESSFULLY
	                    );
				
				$this->view->message = "Invitation(s) have been sent successfully";	
				
			
			
		}
		 	     
		$this->view->userId = $id;
		
	}
	//function to send emails for imported contacts
	public function contactsAction()
	{
		if(!isset($this->oLoggedInUser)){
			$this->_helper->redirector("index","login","default");
		}
		$id = $this->oLoggedInUser->getId();
		
		$request = $this->getRequest();
		
		if($request->isPost())
		{
			
			
				$emailAddress = $request->getPost('invite');
								
				if(is_array($emailAddress))
				{
						
						for($i=0;$i<count($emailAddress);$i++)
						{
							$oChallenge = new Challenge();
							$oChallenge->setUserId($id)
									   ->setEmail($emailAddress[$i])
									   ->setInvitationDate(date('Y-m-d'))
									   ->save();

							$mailDetails = array(
	
												"to"				=>	$emailAddress[$i],
												"subject"			=>	"Challenge for team",
												"challengeid"		=>  $oChallenge->getId(),
												
												
											);
	
						$oMail = new Default_Service_Mail();
						$oMail->sendMail("challenge", $mailDetails);
	
						$this->view->info = array(
	                                                    "type"		=>  "success",
	                                                    "message"	=>	Default_Service_Message::MAIL_SENT_SUCCESSFULLY
	                    );		   
							
						}
						
				}
				
				
				$this->view->message = "Invitation(s) have been sent successfully";	
				
			
			
		}
		 	     
		$this->view->userId = $id;
		
	}
	
	public function joinAction(){
		if(!isset($this->oLoggedInUser))
		{
			 
			 $this->_helper->redirector("index","login","default");
		}
	
		
		$id = $this->_getParam("id"); // get the challenge id and corresponding object
		
		$this->view->userId = $this->oLoggedInUser->getId();
		$this->view->id = $id;
		
		
				$request = $this->getRequest();
				if($request->isPost())
				{
					
					$userID = $this->oLoggedInUser->getId();
					
					 //code to get username from user table			 
					 $oUser = UserPeer::retrieveByPK($userID);
					 $userEmail = $oUser->getUsername(); 
					 
					 //code to get information of logged in user from challnege table 			 
					 $criteria = new Criteria();
					 $criteria->add(ChallengePeer::ID,$id,Criteria::EQUAL);
					 $criteria->add(ChallengePeer::EMAIL,$userEmail,Criteria::EQUAL);
					 $challengeRes = ChallengePeer::doSelectOne($criteria);
					
					 if( count($challengeRes) > 0 )
					 {
						 $oChallenge = ChallengePeer::retrieveByPK($challengeRes->getId());
						 $oChallenge->setAcceptedDate(date('Y-m-d'))
									->setChallengerId($userID)
						 		    ->setIsAccepted('Y')
						 		    ->save();
						 $this->_helper->_redirector("index", "index", "account");			    
					 }

					 
					 		 
				}
				
				
			
			
			
			
		
		
	}
	
	public function acceptAction()
	{
		 $this->_helper->layout()->disableLayout();
		 $status = $this->_getParam('status');
		 $cid = $this->_getParam('cid');
		
		 
		if($status == 'Accept')
		{
			
			$loggedUserId = $this->oLoggedInUser->getId();
			
			$criteria = new Criteria();
			$criteria->add(UserTeamPeer::USER_ID,$loggedUserId,Criteria::EQUAL);
			$userTeamRes = UserTeamPeer::doSelect($criteria);
			
			if(count($userTeamRes) > 0)
			{
				$oChallenge = ChallengePeer::retrieveByPK($cid);
			 	$oChallenge->setAcceptedDate(date('Y-m-d'))
			 		   	 	->setIsAccepted('Y')
							->setChallengerId($loggedUserId)
			 		    	->save();
			 		    	
			    $this->view->msg = 1;		    	
			 		    	
			}
			else 
			{
				$this->view->msg = 0;
				
			}
		}
		else if($status == 'Reject')
		{
				$oChallenge = ChallengePeer::retrieveByPK($cid);
			 	$oChallenge->setAcceptedDate(date('Y-m-d'))
			 		   	 	->setIsAccepted('R')
			 		    	->save();
			 	$this->view->msg = 1;		    	
		}
		
		
	}

//function to check duplicate of loggedin emai Id while sending challneges
	public function checkchallengeemailAction()
	{
	    $this->_helper->viewRenderer->setNoRender(TRUE);
		$this->_helper->layout()->disableLayout();
		$name = $this->_getParam('name');
		$email = explode(",",$name);
		array_unique($email);
		
		$userID = $this->oLoggedInUser->getId();
		$criteria = new Criteria();
		$criteria->add(UserPeer::ID,$userID,Criteria::EQUAL);
		$userRes = UserPeer::doSelectOne($criteria);
		
		$userEmail = $userRes->getUsername();
		for($i=0;$i<count($email);$i++)
		{
			if( $email[$i] == $userEmail )
			{
				echo "You should not enter your email address as you are challenging your friends.";
			}
		}
			
	}
}